#include<iostream>
#include<cstring>
#include<string>
#include<assert.h>
#define size 29989  
#define multiplier 31 
class node  
{
 public:
  char* word;
  int count;  
  node *next;

  node()
    {

      word = NULL;
      count = 0;
      next = NULL;
    };
  ~node(){};
};
node* A[size];

int hash(char* _word);
void insert(char* _word);
void writetxt();
void hashtable();
 void totalhash();

int hash(char* _word)
{
  for(int k = 0; _word[k] != '\0'; k++)
    if(_word[k]>='A' && _word[k]<='Z') _word[k]=_word[k]+32;
  int i = _word[0];
  int hash=i%size;
  return hash;
}

void insert(char* _word)
{
  node *p;
  int i = hash(_word);
  for(p=A[i]; p != NULL; p=p->next)
    {
      if(strcmp(_word,p->word)==0)
	    (p->count)++;
    }
  p = new node;
  p->count = 1;
  p->word =(char*)malloc(strlen(_word)+1);
  strcpy(p->word,_word);
  p->next =A[i];
  A[i] = p;
} 

void writetxt()
{
  FILE *fp;
  char buf[10000];
  char *p= new char[100000];
  fp = fopen("alibaba.txt", "r");
  if(fp == NULL)
    {
      std::cout << "error" << std::endl;
      exit(-1);
    }
  while(NULL != fgets(buf, sizeof(buf), fp))
    {
      buf[strlen(buf) - 1] = '\0';
      if ( buf == "") continue;
      p = strtok(buf, "'\t','\n',',','.',' ','!','?','\"','[',']'");
      while(p != NULL)
	{
	  insert(p);
	  p = strtok(NULL, "'\t','\n',',','.',' ','!','?','\"','[',']'");
	}
    }
  fclose(fp);
}

void hashtable()
{
  FILE *fp;
  node *p;
  int i;
  fp = fopen("number.txt", "w");
  for(i=0;i<size;i++)
    {
      for(p =A[i]; p != NULL; p = p->next)
	{
	  fprintf(fp, "%s: count=%d index=%d\n", p->word, p->count);
	}
    }
  fclose(fp);
  FILE *k;
  k = fopen("mostnumber.txt", "w");
  for(i=0;i<size;i++)
    {
      for(p =A[i]; p != NULL; p = p->next)
	{
	  if(p->count >= 15)
	    fprintf(fp, "%s: count=%d\n", p->word, p->count);
	}
    }
  fclose(k)；

  void totalhash()
  {
      writetxt();
     hashtable();
  }
}
